package com.zmovie.admin.controller.actor;

import com.zmovie.admin.constant.Constants;
import com.zmovie.common.error.BusinessException;
import com.zmovie.common.error.EnumBusinessError;
import com.zmovie.common.response.CommonReturnType;
import com.zmovie.framework.domain.ActorRole;
import com.zmovie.framework.service.ActorRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 演员角色
 * @since 2023-12-04 22:37
 */
@Controller
@RequestMapping("/actor/role")
@Api(tags = "演员角色")
public class ActorRoleController {
    @Autowired
    private ActorRoleService actorRoleService;

    @GetMapping
    @ApiOperation("演员角色管理页面")
    public String index(Model model) {
        List<ActorRole> actorRoles = actorRoleService.list();
        model.addAttribute(Constants.ACTOR_ROLE_KEY, actorRoles);
        return "actor/actor_role";
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取演员角色信息")
    @ResponseBody
    public Object getActorRole(@PathVariable("id") Integer id) throws BusinessException {
        if(id == null){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        return CommonReturnType.success(actorRoleService.getById(id));
    }

    @PostMapping
    @ApiOperation("添加演员角色")
    @ResponseBody
    public Object addActorRole(@Validated ActorRole actorRole){
        actorRoleService.save(actorRole);
        return CommonReturnType.success("添加成功!");
    }

    @PutMapping
    @ApiOperation("修改演员角色")
    @ResponseBody
    public Object updateActorRole(@Validated ActorRole actorRole) throws BusinessException {
        if(actorRole.getId() == null){
            throw new BusinessException(EnumBusinessError.OPERATOR_ERROR);
        }
        actorRoleService.updateById(actorRole);
        return CommonReturnType.success("删除成功!");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除演员角色")
    @ResponseBody
    public Object deleteActorRole(@PathVariable("id") Integer id){
        actorRoleService.removeById(id);
        return CommonReturnType.success("删除成功!");
    }

    @DeleteMapping
    @ApiOperation("批量删除演员角色")
    @ResponseBody
    public Object deleteActorRoleBatch(Integer[] id) throws BusinessException {
        if(id == null || id.length == 0){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        actorRoleService.removeByIds(Arrays.asList(id));
        return CommonReturnType.success("批量删除成功!");
    }

    @PostMapping("/search")
    @ApiOperation("搜索")
    public String search(ActorRole actorRole, Model model){
        List<ActorRole> actorRoles = actorRoleService.listActorRole(actorRole);
        model.addAttribute(Constants.ACTOR_ROLE_KEY, actorRoles);
        return "actor/actor_role :: actorRoleList";
    }
}
